<?php
/*
 * teacher控制器  
 * Tobey
 * */
require_once __ROOT__."/tayphp/Controller.class.php";
class teacherController extends Controller{

	/*
     * 教师修改基本信息的方法
     * ajax   tea_name 和 sex
     * */
	public function update_info(){
		session_start();
		if(IS_AJAX && IS_LOCAL && isset($_SESSION["id"])) {
			$sql = D();
			if($sql->table(array("teacher"=>""))->check($_POST,5)){			//自动验证
				if($sql->table(array("teacher"=>""))->where(array("tea_id"=>$_SESSION["id"]))->update($_POST)){
					$_SESSION["name"] = $_POST["tea_name"];
					$data = $this->message(1,"教师信息修改成功!","");
				} else{
					$data = $this->message(0,"教师信息没有任何变化,无需修改!","");
				}
			} else{
				$data = $this->message(0,$sql->getCheckError(),"");
			}
			unset($sql);
		} else{
			$data = $this->message(0,"非法操作!","");
		}
		$this->ajaxReturn($data);
		session_write_close();
	}

	/*
     * 修改教师密码的方法
     * ajax
     * */
	public function update_pwd(){
		session_start();
		if(IS_AJAX && IS_LOCAL && isset($_SESSION["id"],$_POST["new_pwd"],$_POST["old_pwd"]) && !empty($_POST["new_pwd"])){
			$sql = D();
			$where["tea_id"] = $_SESSION["id"];
			$teacher = $sql->table(array("teacher"=>""))->where($where)->field(array("tea_password_hash"))->find();
			if($teacher && $teacher["tea_password_hash"] == md5($_POST["old_pwd"])){			//身份验证过程
				if($sql->table(array("teacher"=>""))->where($where)->update(array("tea_password_hash"=>md5($_POST["new_pwd"])))){
					$data = $this->message(1,"密码修改成功!","");
				} else{
					$data = $this->message(0,"密码没有任何变化,无需修改!","");
				}
			} else{
				$data = $this->message(0,"身份验证不通过,无法修改密码!","");
			}		
		} else{
			$data = $this->message(0,"非法操作!","");
		}
		$this->ajaxReturn($data);
		session_write_close();
		unset($sql,$where,$teacher,$data);
	}

	/*
     * 教师的注册
     * ajax   tea_email  tea_number tea_password_hash  tea_name  col_id
     * */
	public function add(){
		if(IS_AJAX && IS_LOCAL){
			$sql = D();
			$tea_table = array("teacher"=>"");
			if($sql->table($tea_table)->check($_POST,1)){
				//检查邮箱是否已经注册   工号是否已经注册
				if($sql->table(array("student"=>""))->where(array("stu_email"=>$_POST["tea_email"]))->count() == 0 && $sql->table($tea_table)->where(array("tea_email"=>$_POST["tea_email"]))->count() == 0 && $sql->table($tea_table)->where(array("tea_number"=>$_POST["tea_number"],"col_id"=>$_POST["col_id"]))->count() == 0){
					$_POST["tea_password_hash"] = md5($_POST["tea_password_hash"]);
					if($sql->table($tea_table)->add($_POST)){
						$data = $this->message(1,"教师注册成功!","");
					} else{
						$data = $this->message(0,"教师注册失败!","");
					}
				} else{
					$data = $this->message(0,"邮箱或者工号已经存在,请检查!","");
				}
			} else{
				$data = $this->message(0,$sql->getCheckError(),"");
			}
			$this->ajaxReturn($data);
			unset($sql,$tea_table,$data);
		}
	}

	/*
 	 * 教师注册时,检测一下注册的教工号是否已经存在
 	 * ajax   tea_number   col_id
 	 * */
	public function check_number(){
		if(IS_AJAX && IS_LOCAL) {
			if(D()->table(array("teacher"=>""))->where(array("tea_number"=>$_POST["tea_number"],"col_id"=>$_POST["col_id"]))->count() == 0){
				$data = $this->message(1,"该工号可以进行注册!","");
			} else{
				$data = $this->message(0,"该工号已经存在了!","");
			}
			$this->ajaxReturn($data);
			unset($data);
		}
	}

  	//析构函数不用写任何内容也是可以的
	public function __destruct(){}
}